//
//  CZTranslateTableViewCell.m
//  globalTranslate
//
//  Created by 陈镇 on 2025/8/9.
//

#import "CZTranslateTableViewCell.h"
#import "CZTranslateView.h"
#import "SLPopMenuView.h"
#import "LanguageSelectView.h"


@interface CZTranslateTableViewCell ()


@property(nonatomic,strong) CZTranslateView *topText;
@property(nonatomic,strong) UIImageView *line;
@property(nonatomic,strong) CZTranslateView *bottomText;

@property(nonatomic,strong) QMUIButton *translateBtn;

@end
@implementation CZTranslateTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)didInitializeWithStyle:(UITableViewCellStyle)style{
    [super didInitializeWithStyle:style];



    [self addSubview:self.translateBtn];
    [self.contentView addSubview:self.topText];
    [self.contentView addSubview:self.line];
    [self.contentView addSubview:self.bottomText];
   
   

  
    
    UILongPressGestureRecognizer *press =  [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGestureRecognizer:)];
    [self.contentView addGestureRecognizer:press];
}



-(QMUIButton *)translateBtn{
    if (!_translateBtn) {
        _translateBtn = [QMUIButton createButtonImageName:@"fanyi" selectImageName:@"fanyi"];
        [_translateBtn addTarget:self action:@selector(clickItemFanyi:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _translateBtn;
}

-(CZTranslateView *)topText{
    if (!_topText) {
        _topText = [[CZTranslateView alloc] initWithFrame:CGRectMake(0, 0, MaxWidth_T, 0)];
    }
    return _topText;
}
-(CZTranslateView *)bottomText{
    if (!_bottomText) {
        _bottomText = [[CZTranslateView alloc] initWithFrame:CGRectMake(0, 0, MaxWidth_T, 0)];
    }
    return _bottomText;
}
-(UIImageView *)line{
    if (!_line) {
        _line = [[UIImageView alloc] init];
//        _line.backgroundColor = UIColorTestRed;
        _line.image = [UIImage imageNamed:@"line_iocn"];
        _line.layer.masksToBounds = YES;
        _line.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _line;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
 
    
    
    CGSize tmp_size = [_topText sizeThatFits:CGSizeMake(MaxWidth_T, CGFLOAT_MAX)];
    
    CGSize tmp_size_2 = [_bottomText sizeThatFits:CGSizeMake(tmp_size.width, CGFLOAT_MAX)];
    
    
    if (tmp_size_2.width < tmp_size.width) {
        tmp_size_2.width = tmp_size.width;
    }
    
    if (self.model.isChinese) {
   
        
        self.contentView.frame = CGRectMake(self.headerView.right + 10, 0, tmp_size.width + 30, tmp_size.height + 24 + tmp_size_2.height + 30);
        

        
        
        
        _translateBtn.frame = CGRectMake(self.contentView.right+5, 0, 20, 20);
    }else{
    
        self.contentView.frame = CGRectMake(self.headerView.left - (tmp_size.width + 30) - 10, 0, tmp_size.width + 30, tmp_size.height + 24 + tmp_size_2.height + 30);
        
        _translateBtn.frame = CGRectMake(self.contentView.left - 5 - 20, 0, 20, 20);
    }
    
    
    _topText.frame = CGRectMake(15, 15, tmp_size.width, tmp_size.height);
    
    _line.frame = CGRectMake(15, _topText.bottom + 12, self.contentView.width - 30, 1);
    
    _bottomText.frame = CGRectMake(15, _line.bottom + 12, tmp_size_2.width, tmp_size_2.height);

}
-(CGSize)sizeThatFits:(CGSize)size{
    
    CGSize tmp_size = [_topText sizeThatFits:CGSizeMake(MaxWidth_T, CGFLOAT_MAX)];
    
    CGSize tmp_size_2 = [_bottomText sizeThatFits:CGSizeMake(tmp_size.width, CGFLOAT_MAX)];
    
    return CGSizeMake(SCREEN_HEIGHT , tmp_size.height + 24 +tmp_size_2.height + 30 + 10);
}

-(void)setModel:(CZTranslateModel *)model{
    [super setModel:model];

    _topText.isChinese = model.isChinese;
    _topText.textString = model.orString;
    _topText.textURL = model.speakUrl;

    _bottomText.isChinese =  model.isChinese;
    _bottomText.textString = model.taString;
    _bottomText.textURL = model.tSpeakUrl;


}


- (void)handleLongPressGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        [self becomeFirstResponder];
        UIMenuController *menuController = [UIMenuController sharedMenuController];
        UIMenuItem *copyMenuItem2 = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyString:)];
        
        
        [menuController setMenuItems:@[copyMenuItem2]];
        [menuController showMenuFromView:self.contentView rect:self.contentView.frame];
//        [menuController setMenuVisible:YES animated:YES];
    }
}
-(void)copyString:(id)sender{
    NSString *string = [NSString stringWithFormat:@"%@\n%@",self.model.orString,self.model.taString];
    [[UIPasteboard generalPasteboard] setString:string];
    SLTipCurrenView(@"已复制");
}


-(void)clickItemFanyi:(UIButton *)btn{
    __weak typeof(self) weakSelf = self;
    [CurrentVC.view endEditing:YES];
    [SLPopMenuView showMenuWithView:btn titles:[LanguageSelectView mylanguageList:@"text"] imageNames:@[] block:^(XHPopMenuItem * _Nullable item) {
            
        
        
        NSString *ta_code = [ LanguageSelectView getLanguageCode:item.title type:@"text"];
  
        RTLog(@"%@ - %@",item.title,ta_code);
        
        weakSelf.model.cell_id = [NSDate new].timeIntervalSince1970 * 1000;
        weakSelf.model.ta_code = ta_code;
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangTranslateLanguage" object:weakSelf.model];
    }];
}




@end
